/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: cbus_mutex.h
 *
 * Purpose: the mutex wrapper for cbus
 *
 * Developer:
 *   wen.gu , 2022-01-19
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __CBUS_MUTEX_H__
#define __CBUS_MUTEX_H__

#include "cbus/core/cbus_type.h"
#include "cbus/core/cbus_error.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/

//#define CBUS_MUTEX_INITIALIZER {0}
/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
G_C_HEADER_START

typedef GPTR cbus_mutex_t;
typedef GPTR cbus_condition_variable_t;
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

/**
 * @brief initialize a mutex instance
 * 
 * @param void
 * @return cbus_mutex_t the instance of mutex
 */
G_API cbus_mutex_t cbus_mutex_create(void);

/**
 * @brief destroy a mutex instance
 * 
 * @param mtx [in] mutex instance
 * @return void
 */
G_API void cbus_mutex_destroy(cbus_mutex_t mtx);

/**
 * @brief do lock with mutex instance
 * 
 * @param mtx [in] mutex intance
 * @return void
 */
G_API void cbus_mutex_lock(cbus_mutex_t mtx);

/**
 * @brief do unlock with mutex instance
 * 
 * @param mtx [in] mutex instance
 * @return void
 */
G_API void cbus_mutex_unlock(cbus_mutex_t mtx);

/**
 * @brief initialize a condition variable instance
 * 
 * @param void
 * @return cbus_condition_variable_t the instance of condition variable 
 */
G_API cbus_condition_variable_t cbus_cv_create(void);

/**
 * @brief destroy a condition variable instance
 * 
 * @param cv [in] condition variable instance
 * @return void
 */
G_API void cbus_cv_destroy(cbus_condition_variable_t cv);

/**
 * @brief wait forever with condition variable instance
 * 
 * @param cv [in] condition variable instance
 * @param mtx [in] mutex instance
 * @return void
 */
G_API void cbus_cv_wait(cbus_condition_variable_t cv, cbus_mutex_t mtx);

/**
 * @brief timed wait for condition variable instance
 * 
 * @param cv [in] condition variable instance
 * @param mtx [in] mutex instance
 * @param wait_ms [in] how long to wait,
 * @return GErrc if timeout: G_ErrorTimeout, if success:G_OK 
 */
G_API GErrc cbus_cv_wait_for(cbus_condition_variable_t cv, cbus_mutex_t mtx, GU32 wait_ms);

/**
 * @brief to wake up one condition wait
 * 
 * @param cv [in] condition variable instance
 * @return void 
 */
G_API void cbus_cv_notify_one(cbus_condition_variable_t cv);

/**
 * @brief to wake up all condition wait
 * 
 * @param cv [in] condition variable instance
 * @return void
 */
G_API void cbus_cv_notify_all(cbus_condition_variable_t cv);





G_C_HEADER_END

#endif /** !__CBUS_MUTEX_H__ */

